package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description: 检查项管理
 * @RestController 相当于@Controller+@ResponseBody -->自动将对象转为json格式，通过输出流回写
 * @author chenxys
 * @date 2022/5/18 19:01
 */
@RestController
@RequestMapping(value = "/checkitem")
public class CheckItemController {
    @Reference
    private CheckItemService checkItemService;

    /**
     * 新增检查项
     * 传过来的数据为json，Spring默认解析不了，所以需要添加@RequestBody注解来解析
     * @author chenxys
     * @param checkItem
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result add(@RequestBody CheckItem checkItem){
        System.out.println("123");
        //使用try-catch获知新增方法是否执行成功
        try{
            checkItemService.add(checkItem);
        }catch (Exception e){//如果进入catch，则说明调用失败;反之则成功
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        //新增成功
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * 分页查询 接收json数据参数列表必须加@RequestBody注解
     * @author chenxys
     * @param queryPageBean
     * @return com.itheima.entity.PageResult
     */
    @RequestMapping(value = "/pageQuery")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        //因为添加@RestController注解，会自动将对象转换为json数据格式并返回给前端
        return checkItemService.pageQuery(queryPageBean);
    }

    /**
     * 删除检查项
     * @author chenxys
     * @param id
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/delete")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result delete(Integer id){
        try{
            checkItemService.delete(id);
        }catch (RuntimeException e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 根据ID查询
     * @author chenxys
     * @param id
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/findById")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findById(Integer id){
        try {
            CheckItem checkItem = checkItemService.findById(id);
            //查询成功
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
        }catch (Exception e){
            e.printStackTrace();
            //查询失败
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 编辑检查项
     * @author chenxys
     * @param checkItem
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/edit")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result edit(@RequestBody CheckItem checkItem){
        try {
            checkItemService.edit(checkItem);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    /**
     * 查询所有检查项
     * @author chenxys
     * @param
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/findAll")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findAll(){
        List<CheckItem> checkItemList = checkItemService.findAll();
        if (checkItemList != null && checkItemList.size() > 0){
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);
        }
        return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
    }

}
